#include "reader.h"

#include <QDebug>
#include <QThread>

#include "utils.h"

template<>
size_t LibraryClient<'R'>::N = 0;

Reader::Reader(ReadOnlyLibrary& library) :
    _library(library)
{
}

void Reader::runOnce()
{
    const size_t maxIndex = _library.getBookCount();

    if (maxIndex == 0) {
        qDebug() << "R" << _id << " library is empty :(";
        sleep(_library.readInterval());
        return;
    }

    size_t index = random(maxIndex);

    qDebug() << "R" << _id << " want to read the Book #" << index;
    book_t book = _library.getBook(index);

    qDebug() << "R" << _id << " started reading the Book #" << index;
    sleep(_library.readInterval());

    qDebug() << "R" << _id << " finished reading the Book #" << index;
    _library.returnBook(book);
}
